package com.hxkcloud.patient.aerotel;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.support.v7.app.AppCompatActivity;
import android.text.format.DateUtils;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;


import com.hxkcloud.patient.R;
import com.hxkcloud.patient.aerotel.domain.health.AerotelECGPdu;
import com.hxkcloud.patient.aerotel.domain.health.ECGManager;
import com.hxkcloud.patient.aerotel.util.DateUtil;
import com.hxkcloud.patient.aerotel.view.ECGGridView;

import java.text.ParseException;
import java.util.Date;

public class ECGCanvasActivity extends AppCompatActivity {

    private AerotelECGPdu pdu = null;

    private ECGGridView view = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ecgcanvas);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        if (NavUtils.getParentActivityName(this) != null) {
            this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }

        view = (ECGGridView) findViewById(R.id.canvas);

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        int gsize = dm.widthPixels / 50;
        int height = gsize * 60;
        ViewGroup.LayoutParams layoutp = view.getLayoutParams();
        layoutp.height = height;
        layoutp.width = dm.widthPixels;
        view.setLayoutParams(layoutp);
    }

    @Override
    protected void onStart() {
        super.onStart();
        long id = this.getIntent().getExtras().getLong("id");
        pdu = ECGManager.getFromCache(id);
        if (pdu != null) {
            pdu.loadfile(this);
            view.setEcg(pdu);
        }

//        byte[] header = this.getIntent().getByteArrayExtra("ecg.header");
//        byte[] data = this.getIntent().getByteArrayExtra("ecg.data");
//
//        try {
//            XmlResourceParser datap = getResources().getXml(R.xml.hrr);
//            int event = datap.getEventType();
//            while (event != XmlResourceParser.END_DOCUMENT) {
//                switch (event) {
//                    case XmlResourceParser.START_DOCUMENT:
//                        break;
//                    case XmlPullParser.START_TAG:
//                        if ("ecg".equals(datap.getName())) {
//                            String dstr = datap.nextText();
//                            if (StringUtil.isNotNullOrEmpty(dstr)) {
//                                AppLogger.d(dstr.length() + ":" + dstr);
//                                data = HexUtil.toBytes(dstr);
//                            }
//                        }
//                        break;
//                    default:
//                        break;
//                }
//                event = datap.next();
//            }
//        } catch (XmlPullParserException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//        header = new byte[60];
//        if (header != null && data != null) {
//            pdu = new AerotelECGPdu();
//            pdu.header = new AerotelECGPdu.Header(header);
//            pdu.data = new AerotelECGPdu.Data(data);
//            if (pdu.data.inflate(data)) {
//                view.setEcg(pdu);
//            }
//        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_ecgcanvas, menu);

        MenuItem mi = menu.findItem(R.id.action_date);
        if (mi != null) {
            try {
                Date d = DateUtil.parse(pdu.createdDate, DateUtil.DATETIME_FORMAT_S);
                pdu.createdDate = (String) DateUtils.getRelativeTimeSpanString(d.getTime());
            } catch (ParseException e) {
                e.printStackTrace();
            }
            mi.setTitle(pdu.createdDate);
        }
        MenuItem mid = menu.findItem(R.id.action_device);
        if (mid != null) {
            mid.setTitle(pdu.devicename);
        }
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){
            case android.R.id.home:
                if (NavUtils.getParentActivityName(this) != null) {
                    NavUtils.navigateUpFromSameTask(this);
                }
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
